﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using AMM.Model;

namespace AMM.DataAccess.Config {
    public class BasicsRepository : BaseRepository<Basic> {

        public Basic FetchBasicsById(int basicsId) {
            using (var ctx = this.Context) {
                return ctx.Basics.Find(basicsId);
            }
        }

        public List<Basic> FetchBasicsByType(int typeEnum) {
            using (var ctx = this.Context) {
                return ctx.Basics.Where(x => x.TypeEnum == typeEnum).OrderBy(x => x.Sequence).ToList();
            }
        }

        public void Delete() {
            using (var ctx = this.Context) {
                ctx.Basics
                    .Where(x => x.IsSystem == false && x.IsEnabled == false)
                    .ToList()
                    .ForEach(x => {
                        ctx.Basics.Attach(x);
                        ctx.Entry(x).State = EntityState.Deleted;
                    });
                ctx.SaveChanges();
            }
        }
    }
}
